package org.leyi.shop.util;
import com.google.gson.Gson;
import com.google.gson.JsonParseException;
import org.apache.commons.lang3.StringUtils;
import org.leyi.shop.common.exception.BusinessException;
import org.leyi.shop.common.response.EnumResponse;

/**
 * 
 * description:  各种校验工具 
 * @author  lenovo
 * create 2024/5/5-23:00 */
public class Verifier {
    static final String EX4 = "^(?![A-Za-z0-9]+$)(?![a-z0-9\\W]+$)(?![A-Za-z\\W]+$)(?![A-Z0-9\\W]+$)[a-zA-Z0-9\\W]+$";
    static final String EX3 = "^(?![a-zA-Z]+$)(?![A-Z0-9]+$)(?![A-Z\\W_]+$)(?![a-z0-9]+$)(?![a-z\\W_]+$)(?![0-9\\W_]+$)[a-zA-Z0-9\\W_]+$";
    static final String EX2 ="^[a-zA-Z0-9]*$";
    static final String EX1 ="^(?=.*[a-zA-Z])(?=.*[0-9])[a-zA-Z0-9]*$";

    /**
     *
     * description: 判断字符串里是否同时包含数字和字母
     * @param str 待校验文本
     * @return boolean
     * @author  lenovo
     * create 2024/5/5-23:50
     */
    public  static boolean containsNumAndAlpha(String str)
    {
        return str.matches(EX1);
    }

    /**
     *
     * description: 判断字符串里是否包含字母或者数字
     * @param str 待校验文本
     * @return boolean
     * @author  lenovo
     * create 2024/5/5-23:53
     */
    public static boolean onlyContainsNumAndAlpha(String str)
    {
        return str.matches(EX2);
    }

    /**
     *
     * description: 判断字符串里是否至少包含大写字母、小写字母、数字、特殊符号的其中三种
     * @param str 待校验文本
     * @return boolean
     * @author  lenovo
     * create 2024/5/5-23:55
     */
    public static boolean containsAtLeastAnyThreeOfLettersNumbersSymbols(String str)
    {
        return str.matches(EX3);
    }

    /**
     *
     * description:  判断字符串里是否至少包含大写字母、小写字母、数字、特殊符号的其中四种
     * @param str 待校验文本
     * @return boolean
     * @author  lenovo
     * create 2024/5/5-23:57
     */
    public static boolean containsAtLeastAnyFourOfLettersNumbersSymbols(String str)
    {
        return str.matches(EX4);
    }

    /**
     *
     * description: 验证是不是合法的json字符串
     * @param json json字符串
     * @return boolean
     * @author  lenovo
     * create 2024/5/5-23:57
     */
    public static boolean isValidJsonString(String json) {
        if (StringUtils.isBlank(json)) {
            return false;
        }
        try {
            new Gson().fromJson(json,Object.class);
            return true;
        } catch (JsonParseException e) {
            throw new BusinessException(EnumResponse.PARAMETER_ERR,"json字符串格式有问题","");
        }}
}

